<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>使用 MongoCollection::findOne 方法</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.insert.html">插入一个文档</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.insert.multiple.html">添加多个文档</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.findone" class="section">
   <h2 class="title">
    使用 <span class="function"><a href="mongocollection.findone.html" class="function">MongoCollection::findOne()</a></span> 方法
   </h2>
   <p class="para">
    要查看我们上一部插入到数据库的文档，可以简单的使用
    <span class="function"><a href="mongocollection.findone.html" class="function">MongoCollection::findOne()</a></span>
    方法从即合理获得一个简单的文档。
    这个方法在只想查询一个结果的时候很有用。
   </p>
   <div class="example" id="mongo.tutorial.findone-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$document&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">findOne</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$document&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(6) {
  [&quot;_id&quot;]=&gt;
  object(MongoId)#8 (1) {
    [&quot;$id&quot;]=&gt;
    string(24) &quot;4e2995576803fab768000000&quot;
  }
  [&quot;name&quot;]=&gt;
  string(7) &quot;MongoDB&quot;
  [&quot;type&quot;]=&gt;
  string(8) &quot;database&quot;
  [&quot;count&quot;]=&gt;
  int(1)
  [&quot;info&quot;]=&gt;
  array(2) {
    [&quot;x&quot;]=&gt;
    int(203)
    [&quot;y&quot;]=&gt;
    int(102)
  }
  [&quot;versions&quot;]=&gt;
  array(3) {
    [0]=&gt;
    string(5) &quot;0.9.7&quot;
    [1]=&gt;
    string(5) &quot;0.9.8&quot;
    [2]=&gt;
    string(5) &quot;0.9.9&quot;
  }
}
</pre></div>
    </div>
   </div>
   <p class="para">
    注意：有一个 <em>_id</em> 字段被自动添加到你的文档中了。
    <em>_id</em> 字段就是集合的“主键”。
    如果插入文档的时候你没有手动指定，驱动就会自动帮你添加一个。
   </p>
   <p class="para">
    如果你所插入的文档定义了 <em>_id</em> 字段，那么它在集合中必须是唯一的。
    这是一个例子：
   </p>
   <div class="example" id="mongo.tutorial.findone-example-2">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;will&nbsp;throw&nbsp;an&nbsp;exception<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;is&nbsp;fine,&nbsp;as&nbsp;it&nbsp;is&nbsp;a&nbsp;different&nbsp;collection<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <p class="para">
    默认设置时，驱动会在服务器通过了写入请求后返回（译注：“通过”即如果不发生崩溃等情况，待插入的文档就一定会在随后被写入，这并不意味着数据已经写入磁盘）。你可以通过将第二个参数设为 <em>array(&quot;w&quot; =&gt; 0)</em>
    来改变默认行为。此时你的插入请求会立即返回，并且不会抛出 <em>_id</em> 重复的异常。
   </p>
   <div class="section" id="mongo.tutorial.findone.seealso">
    <h2 class="title">参见</h2>
    <p class="para">
     <span class="function"><a href="mongocollection.findone.html" class="function">MongoCollection::findOne()</a></span>
     方法的文档中有对查询的详细说明。
    </p>
    <p class="para">
     唯一ID的信息查看 <a href="class.mongoid.html" class="classname">MongoId</a>
    </p>
    <p class="para">
     <a href="mongo.writes.html" class="link">写入</a> 部分更详细的说明了写操作,
     <a href="mongo.writeconcerns.html" class="xref">Write Concerns</a> 章节深入解释了一些写操作的选项。
    </p>
   </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.insert.html">插入一个文档</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.insert.multiple.html">添加多个文档</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
